package studychinese.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.Url;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;
import studychinese.model.WhyStudayBean;
import studychinese.utils.ListToStringUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class WhyStudayCZFragment extends BaseFragment {

    @Bind(R.id.tv_native_titile)
    TextView tvNativeTitile;
    @Bind(R.id.fl_native_whystuday_cz)
    TagFlowLayout flNativeWhystudayCz;
    private TagAdapter<String> mAdapter ;
    private LayoutInflater mInflater;
    private String str="";


    public WhyStudayCZFragment() {
        // Required empty public constructor
    }

    public static WhyStudayCZFragment newInstance() {
        
        Bundle args = new Bundle();
        
        WhyStudayCZFragment fragment = new WhyStudayCZFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_why_studay_cz, container, false);
        ButterKnife.bind(this, rootView);
        mInflater=inflater;
        setData();

        return rootView;
    }

    private void setData() {
        OkGo.<WhyStudayBean>post(AppUrl.getAppUrl(Url.REGISTER_USEROBJECTIVE))
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST).execute(new DialogCallback<WhyStudayBean>(getActivity(),true) {
            @Override
            public void onSuccess(Response<WhyStudayBean> response) {
                WhyStudayBean body = response.body();
                if("0".equals(body.getState())){
                    List<WhyStudayBean.DataBean> data = body.getData();
                    setView(data);
                }

            }
        });
    }

    private void setView(List<WhyStudayBean.DataBean> data) {
        ArrayList<String> list = new ArrayList<>();
        list.clear();
        for (int d = 0; d <data.size() ; d++) {
           list.add(data.get(d).getName());

        }
        flNativeWhystudayCz.setAdapter(mAdapter = new TagAdapter<String>(list)
        {

            @Override
            public View getView(FlowLayout parent, int position, String s)
            {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        flNativeWhystudayCz, false);

                tv.setText(s);
                return tv;
            }
        });
        mAdapter.setSelectedList();
        //获得所有选中的pos集合
        flNativeWhystudayCz.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
        {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent)
            {
                //Toast.makeText(getActivity(), mVals[position], Toast.LENGTH_SHORT).show();
                //view.setVisibility(View.GONE);
                return true;
            }
        });



        flNativeWhystudayCz.setOnSelectListener(new TagFlowLayout.OnSelectListener()
        {
            @Override
            public void onSelected(Set<Integer> selectPosSet)
            {
              RegisterBean.DataBean infor = UserBean.getInfor();
                if(selectPosSet.size()>0){
                    List<Integer> integers = new ArrayList<>(selectPosSet);
                    str = ListToStringUtil.listToString1(integers, ",");
                }else {
                    str="";
                }
                infor.setInterest(str);
                UserBean.putUserBean(infor);
            }
        });
        Set<Integer> selectedList = flNativeWhystudayCz.getSelectedList();
        if(list.size()>0){
            RegisterBean.DataBean infor = UserBean.getInfor();
            infor.setInterest(list.get(0));
            UserBean.putUserBean(infor);
        }
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
